package com.feng.one;

public class CreateAndDisplay {
    public static void main(String[] args) {
        //创建结点
        HeroNode node1 = new HeroNode(101,"宋江","及时雨");
        HeroNode node2 = new HeroNode(102,"卢俊义","玉麒麟");
        HeroNode node3 = new HeroNode(103,"吴用","智多星");

        //创建单链表
        SingleLinkedList linkedList = new SingleLinkedList();
        //添加结点
        linkedList.add(node1);
        linkedList.add(node3);
        linkedList.add(node2);

        //遍历链表
        linkedList.Display();
    }
}


//定义结点
class HeroNode{
    public int no;
    public String name;
    public String nikeName;
    public HeroNode next;//指向下一个结点

    public HeroNode(int no, String name, String nikeName) {
        this.no = no;
        this.name = name;
        this.nikeName = nikeName;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nikeName='" + nikeName + '\'' +
                '}';
    }
}

//定义单链表
class SingleLinkedList{
    //初始化头结点
    private HeroNode head = new HeroNode(0,"","");

    //添加结点到链表
    public void add(HeroNode heroNode){
        //head头结点不能动，需要一个辅助遍历
        HeroNode temp = head;
        //遍历找到最后一个结点
        while (true){
            if (temp.next==null){
                break;
            }
            temp=temp.next;
        }
        //刚刚最后一个结点找到了，添加结点到最后
        temp.next=heroNode;
    }

    //显示链表【遍历】
    public void Display(){
        if (head.next==null){
            System.out.println("链表为空");
            return;
        }
        HeroNode temp = head.next;
        while (true){
            if (temp==null){
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }
}